<% include ../header.ejs %>

<link rel="stylesheet" href="/stylesheets/app/recommendadd/recommendadd.css">

<header data-am-widget="header" data-am-sticky class="am-header am-header-default" id="appTemplates-Nav">
    <div class="am-header-left am-header-nav">
        <a href='javascript:history.go(-1)' class="">
            <img class="am-header-icon-custom" src="data:image/svg+xml;charset&#x3D;utf-8,&lt;svg xmlns&#x3D;&quot;http://www.w3.org/2000/svg&quot; viewBox&#x3D;&quot;0 0 12 20&quot;&gt;&lt;path d&#x3D;&quot;M10,0l2,2l-8,8l8,8l-2,2L0,10L10,0z&quot; fill&#x3D;&quot;%23fff&quot;/&gt;&lt;/svg&gt;" alt=""/>
        </a>
    </div>

    <h1 class="am-header-title">
        <a href="#title-link" class="" id="Typeface-whitecolor">重点推荐详情</a>
    </h1>

    <% if(recommend.createUid==uid){ %>
    <div class="am-header-right am-header-nav">
        <a href="#right-link" class="" id="doc-edit-toggle">
            编辑
        </a>
    </div>
    <%}%>
</header>

<form class="am-form" id="doc-vld-msg">
    <fieldset disabled id="formFS">
        <div class="am-form-group">
            <label for="doc-vld-name">卖方(必填)：</label>
            <select  data-am-selected="{btnWidth: '100%'}" id="seller" placeholder="<%= recommend.seller.name %>" required>
                <% sellerlist.forEach(function(data, key){ %>
                    <% if(recommend.seller.rowId==data.rowId){ %>
                    <option value="<%= data.rowId %>" selected><%= data.name %></option>
                    <%} else {%>
                    <option value="<%= data.rowId %>"><%= data.name %></option>
                    <%}%>
                <% }) %>
            </select>
            <span class="am-form-caret"></span>
        </div>
        <div class="am-form-group">
            <label for="doc-vld-name">代码(必填)：</label>
            <input type="text" class="" id="stockCode" placeholder="代码" value="<%= recommend.stockCode%>" required>
        </div>

        <div class="am-form-group">
            <label for="doc-vld-name">目标价(必填)：</label>
            <input type="number" class="" id="targetPrice" placeholder="目标价" value="<%= recommend.targetPrice%>" required>
        </div>

        <div class="am-form-group">
        <label for="doc-vld-name">日期(必填)：</label>
        <div class="am-input-group am-datepicker-date" data-am-datepicker="{format: 'yyyy-mm-dd', viewMode: 'years'}">
            <input type="text" class="am-form-field" placeholder="选择日期" value="<%= moment(recommend.sellingTime).format('YYYY-MM-DD') %>" id="sellingTime" readonly required>
            <span class="am-input-group-btn am-datepicker-add-on">
                <button class="am-btn am-btn-default" type="button"><span class="am-icon-calendar"></span> </button>
            </span>
        </div>
        </div>

        <div class="am-form-group">
            <label for="doc-vld-name">观点：</label>
            <textarea class="" rows="5" id="content" placeholder="观点"><%= recommend.content%></textarea>
        </div>

        <!--<% if(recommend.createUid==uid){ %>-->
         <!--<button type="button" class="am-btn am-btn-primary am-btn-block" id="doc-delete-toggle">删除</button>-->
        <!--<%}%>-->

        <div id="divAction" style="display:none">
            <button class="am-btn am-btn-danger" type="button" id="doc-delete-toggle">删除</button>
            <button class="am-btn am-btn-secondary" type="button" id="doc-save-toggle">保存</button>
        </div>

    </fieldset>
</form>

<div class="am-modal am-modal-confirm" tabindex="-1" id="my-delete">
    <div class="am-modal-dialog">
        <div class="am-modal-bd">
            是否确定删除
        </div>
        <div class="am-modal-footer">
            <span class="am-modal-btn" data-am-modal-cancel>取消</span>
            <span class="am-modal-btn" data-am-modal-confirm>确定</span>
        </div>
    </div>
</div>

<div class="am-modal am-modal-confirm" tabindex="-1" id="my-save">
    <div class="am-modal-dialog">
        <div class="am-modal-bd">
            是否确定更新
        </div>
        <div class="am-modal-footer">
            <span class="am-modal-btn" data-am-modal-cancel>取消</span>
            <span class="am-modal-btn" data-am-modal-confirm>确定</span>
        </div>
    </div>
</div>

<div class="am-modal am-modal-loading am-modal-no-btn" tabindex="-1" id="my-modal-loading">
    <div class="am-modal-dialog">
        <div class="am-modal-hd">正在载入...</div>
        <div class="am-modal-bd">
            <span class="am-icon-spinner am-icon-spin"></span>
        </div>
    </div>
</div>

<div class="am-modal am-modal-alert" tabindex="-1" id="my-alert">
    <div class="am-modal-dialog">
        <div class="am-modal-bd" id ="alert-msg">
        </div>
        <div class="am-modal-footer">
            <span class="am-modal-btn">确定</span>
        </div>
    </div>
</div>



<script>
    var isEdit=false;

    var oldseller= $('#seller').val();
    var oldstockCode=$('#stockCode').val();
    var oldtargetPrice=$('#targetPrice').val();
    var oldsellingTime=$('#sellingTime').val();
    var oldcontent=$('#content').val();

    function reset(){
        $('#seller').val(oldseller);
        $('#stockCode').val(oldstockCode);
        $('#targetPrice').val(oldtargetPrice);
        $('#sellingTime').val(oldsellingTime);
        $('#content').val(oldcontent);
    }

    function showButton(isVisible){
        $('#divAction').toggle(200);
    }


    //编辑功能
    $(function() {
        $('#doc-edit-toggle').on('click', function() {
            if(isEdit){
                isEdit=false;
                $("#doc-edit-toggle").text("编辑");
                showButton(false);
                reset();
                $('#formFS').attr("disabled",true);
            }
            else{
                isEdit=true;
                $("#doc-edit-toggle").text("取消");
                showButton(true);
                $('#formFS').attr("disabled",false);
            }
        });
    });


    //删除功能
    $(function() {
        $('#doc-delete-toggle').on('click', function() {
            $('#my-delete').modal({
                relatedTarget: this,
                onConfirm: function(options) {
                    $('#my-modal-loading').modal('open')

                    var options = {
                        url: 'recommenddelete',
                        type: 'post',
                        dataType: 'json',
                        data: {
                            'rowId': '<%= recommend.rowId%>'
                        },
                        error: function (request) {
                            $('#my-modal-loading').modal('close')
                            $('#alert-msg').html("删除失败")
                            $('#my-alert').modal('open')
                        },
                        success: function (data) {
                            $('#my-modal-loading').modal('close')
                            if(data.code==200){
                                location.href = "../recommend/recommendindex"
                            }else{
                                $('#alert-msg').html("删除失败")
                                $('#my-alert').modal('open')
                            }
                        }
                    };
                    $.ajax(options);

                },
                // closeOnConfirm: false,
                onCancel: function() {
                }
            });
        });
    });

    //更新功能
    $(function() {
        $('#doc-save-toggle').on('click', function() {
            var formValidity =  $('#doc-vld-msg').validator('isFormValid')

            if(formValidity) {
                $('#my-save').modal({
                    relatedTarget: this,
                    onConfirm: function (options) {
                        if ($('#seller').val() == "") {
                            alert("卖家不能为空")
                            return
                        }

                        if ($('#stockCode').val() == "") {
                            alert("代码不能为空")
                            return
                        }

                        if ($('#targetPrice').val() == "") {
                            alert("目标价不能为空")
                            return
                        }

                        if ($('#sellingTime').val() == "") {
                            alert("时间不能为空")
                            return
                        }

                        $('#my-modal-loading').modal('open')

                        var options = {
                            url: 'recommendupdate',
                            type: 'post',
                            dataType: 'json',
                            data: {
                                'rowId': '<%= recommend.rowId%>',
                                'seller': $('#seller').val(),
                                'stockCode': $('#stockCode').val(),
                                'targetPrice': $('#targetPrice').val(),
                                'sellingTime': $('#sellingTime').val(),
                                'content': $('#content').val()
                            },
                            error: function (request) {
                                $('#my-modal-loading').modal('close')
                                $('#alert-msg').html("更新失败")
                                $('#my-alert').modal('open')
                                $('#doc-vld-msg').validator('destroy')
                            },
                            success: function (data) {
                                $('#my-modal-loading').modal('close')
                                if (data.code == 200) {
                                    location.href = "../recommend/recommendindex"
                                } else {
                                    $('#alert-msg').html("更新失败")
                                    $('#my-alert').modal('open')
                                }
                                $('#doc-vld-msg').validator('destroy')
                            }
                        };
                        $.ajax(options);

                    },
                    // closeOnConfirm: false,
                    onCancel: function () {
                        $('#doc-vld-msg').validator('destroy')
                    }
                });
            }else {

            }

        });
    });


    //js表单验证
    $(function() {
        $('#doc-vld-msg').validator({
            onValid: function(validity) {
                $(validity.field).closest('.am-form-group').find('.am-alert').hide();
            },

            onInValid: function(validity) {
                var $field = $(validity.field);
                var $group = $field.closest('.am-form-group');
                var $alert = $group.find('.am-alert');
                // 使用自定义的提示信息 或 插件内置的提示信息
                var msg = $field.data('validationMessage') || this.getValidationMessage(validity);

                if (!$alert.length) {
                    $alert = $('<div class="am-alert am-alert-danger"></div>').hide().
                            appendTo($group);
                }

                $alert.html(msg).show();
            }
        });
    });
</script>


<% include ../footer.ejs %>